package com.blt.govdata.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.blt.govdata.model.TProjectAgreement;
import com.blt.govdata.service.ITProjectAgreementService;
import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 * @author AustinGJ123
 * @since 2018-09-16
 */
@RestController
@RequestMapping("/tProjectAgreement")
//todo 将所有的XXX修改为真实值
public class TProjectAgreementController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ITProjectAgreementService service;

    /**
     * 通过项目ID查询
     */
    @RequestMapping(value = "/selectByPid", params = {"pId"})
    @ResponseBody
    public Map<String, Object> list(Integer pId) {

        return ImmutableMap.of("status", "true", "data", this.service.selectList(BeanUtils.instantiate(EntityWrapper.class).where("p_id = {0}", pId)));

    }


    /**
     * 新增
     */
    @RequestMapping(value = "addAgree", params = {"pId"})
    public Map<String, Object> addAgree(TProjectAgreement after) {
        boolean result = this.service.insert(after);
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 修改
     */

    @RequestMapping(value = "editAgree", params = {"id"})
    public Map<String, Object> editAgree(TProjectAgreement after) {
        boolean result = this.service.updateById(after);
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 删除
     */

    @RequestMapping(value = "delete", params = {"id"})
    public Map<String, Object> delete(Integer id) {
        boolean result = this.service.deleteById(id);
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 新增或修改
     */
    @RequestMapping(value = "createOrModifyAgree", params = {"pId"})
    public Map<String, Object> createOrModifyAfter(TProjectAgreement agree) {
        TProjectAgreement one = this.service.selectOne(BeanUtils.instantiate(EntityWrapper.class).where("p_id = {0}", agree.getpId()).and("id = {0}",agree.getId()));
        if (null == one) {
            return addAgree(agree);
        }
        return editAgree(agree);
    }
}
